/**
 * 严选商品服务类
 * 用于获取严选商品列表、商品详情等商品相关操作
 */

const request = require('../libs/request');

class GoodsService {
	/**
	 * 获取严选商品列表
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码
	 * @param {number} params.limit 每页数量
	 * @param {number} params.cat_id 分类ID
	 * @param {number} params.sub_cat_id 子分类ID
	 * @returns {Promise}
	 */
	static async getGoodsList(params = {}) {
		const { page = 1, limit = 10, cat_id = 0, sub_cat_id = 0 } = params;
		
		try {
			const response = await request.get('/v1/goods/index', {
				needAuth: false,
				data: {
					page,
					limit,
					cat_id,
					sub_cat_id
				}
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取严选商品列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取严选商品列表失败'
			};
		}
	}

	/**
	 * 获取商品分类树
	 * @returns {Promise}
	 */
	static async getCategories() {
		try {
			const response = await request.get('/v1/goods/categories', {
				needAuth: false
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取商品分类成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商品分类失败'
			};
		}
	}

	/**
	 * 获取指定分类的二级分类列表
	 * @param {number} catId 一级分类ID
	 * @returns {Promise}
	 */
	static async getSubCategories(catId) {
		if (!catId) {
			return {
				success: false,
				data: null,
				message: '分类ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/sub-categories', {
				needAuth: false,
				data: { cat_id: catId }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取二级分类成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取二级分类失败'
			};
		}
	}

	/**
	 * 获取商品详情
	 * @param {number} id 商品ID
	 * @returns {Promise}
	 */
	static async getGoodsDetail(id) {
		if (!id) {
			return {
				success: false,
				data: null,
				message: '商品ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/one', {
				needAuth: false,
				data: { id }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取商品详情成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商品详情失败'
			};
		}
	}

	/**
	 * 搜索商品
	 * @param {Object} params 搜索参数
	 * @param {string} params.keyword 搜索关键词
	 * @param {number} params.page 页码
	 * @param {number} params.limit 每页数量
	 * @returns {Promise}
	 */
	static async searchGoods(params = {}) {
		const { keyword = '', page = 1, limit = 10 } = params;
		
		try {
			const response = await request.get('/v1/goods/search', {
				needAuth: false,
				data: {
					keyword,
					page,
					limit
				}
			});

			return {
				success: response.code === 1,
				data: response.data,
				total: response.total,
				message: response.msg || '搜索商品成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '搜索商品失败'
			};
		}
	}

	static async buyGoods(params = {}) {
		try {
			const response = await request.post('/v1/goods/buy', {
				needAuth: true,
				data: params
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '购买商品成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}

	/**
	 * 根据商品ID和规格组合获取SKU信息（价格和库存）
	 * @param {number} goodsId 商品ID
	 * @param {string} specs 规格组合字符串，格式如："红色@XL@棉质"
	 * @returns {Promise}
	 */
	static async getSkuBySpecs(goodsId, specs) {
		if (!goodsId) {
			return {
				success: false,
				data: null,
				message: '商品ID不能为空'
			};
		}

		if (!specs) {
			return {
				success: false,
				data: null,
				message: '规格组合不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/get-sku-by-specs', {
				needAuth: false,
				data: {
					goods_id: goodsId,
					specs: specs
				}
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取SKU信息成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取SKU信息失败'
			};
		}
	}

	/**
	 * 根据商品ID获取所有SKU列表
	 * @param {number} goodsId 商品ID
	 * @returns {Promise}
	 */
	static async getSkuList(goodsId) {
		if (!goodsId) {
			return {
				success: false,
				data: null,
				message: '商品ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/get-sku-list', {
				needAuth: false,
				data: { goods_id: goodsId }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取SKU列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取SKU列表失败'
			};
		}
	}

	/**
	 * 根据SKU编码获取SKU信息
	 * @param {string} skuCode SKU编码
	 * @returns {Promise}
	 */
	static async getSkuByCode(skuCode) {
		if (!skuCode) {
			return {
				success: false,
				data: null,
				message: 'SKU编码不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/get-sku-by-code', {
				needAuth: false,
				data: { sku_code: skuCode }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取SKU信息成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取SKU信息失败'
			};
		}
	}

	/**
	 * 获取严选订单支付信息（云豆余额、福利卡等）
	 * @param {number} orderId 订单ID
	 * @returns {Promise}
	 */
	static async getPayInfo(orderId) {
		if (!orderId) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/get-pay-info', {
				needAuth: true,
				data: { order_id: orderId }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '获取支付信息成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取支付信息失败'
			};
		}
	}

	/**
	 * 创建严选订单支付记录
	 * @param {Object} params 支付参数
	 * @param {number} params.order_id 订单ID
	 * @param {number} params.use_score 使用云豆数量
	 * @param {number} params.use_card_score 使用福利卡云豆数量
	 * @param {Array} params.selected_cards 选中的福利卡列表
	 * @returns {Promise}
	 */
	static async createPay(params = {}) {
		if (!params.order_id) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		try {
			const response = await request.post('/v1/goods/create-pay', {
				needAuth: true,
				data: params
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '创建支付记录成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '创建支付记录失败'
			};
		}
	}

	/**
	 * 查询严选订单支付状态
	 * @param {string} payRecordId 支付记录ID
	 * @returns {Promise}
	 */
	static async queryPayStatus(payRecordId) {
		if (!payRecordId) {
			return {
				success: false,
				data: null,
				message: '支付记录ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/goods/query-pay-status', {
				needAuth: true,
				data: { pay_record_id: payRecordId }
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.msg || '查询支付状态成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '查询支付状态失败'
			};
		}
	}

	static async recommends(){
        try {
            const response = await request.get('/v1/goods/recommends', {
                needAuth: false,
            });
            
            return {
                success: response.code === 1,
                data: response.data,
                message: response.msg || '获取推荐商品成功'
            };
        } catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取推荐商品失败'
			};
		}
	}   

	static async pings(params = {}) {
		const { id, showLoading = false } = params;
		try {
			const response = await request.get(`/v1/goods/pings`, {
				showLoading,
				needAuth: false,
				data: {
					id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家商品列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商家商品列表失败'
			};
		}
	}
	
}

module.exports = GoodsService; 